package com.zhuoshi.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zhuoshi.entity.Role;
import com.zhuoshi.entity.User;
import com.zhuoshi.entity.UserCustom;
import com.zhuoshi.service.RoleService;
import com.zhuoshi.service.UserService;

@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	//登录
	@RequestMapping("login")
	public ModelAndView login(HttpSession session,User user) throws Exception{
		User user1=userService.longin(user);
		ModelAndView modelAndView=new ModelAndView();
		String result="";
		if(user1!=null){
			result="/Admin/index.jsp";
			session.setAttribute("user1", user1);
		}else{
			result="/Admin/login.jsp";
		}
		modelAndView.setViewName(result);
		return modelAndView;
	}
	
	//退出登录
	@RequestMapping("logout")
	public String logout(HttpSession session) throws Exception{
		session.invalidate();
		return "/Admin/login.jsp";
		
	}
			
	//所有用户显示页面
	@RequestMapping("selectUserAllYao")
	public ModelAndView selectUserAllYao(String useraccount) throws Exception{
		List<UserCustom> userList=userService.selectUserAllYao(useraccount);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("userList",userList);
		modelAndView.setViewName("/Admin/userManager.jsp");
		return modelAndView;
	}
	
	//用户信息填写
	@RequestMapping("addUserMessageLiu")
	public ModelAndView addUserMessageLiu() {
		List<Role> roles=roleService.selectAllRoleLiu();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("roles",roles);
		modelAndView.setViewName("/Admin/addUser.jsp");
		return modelAndView;
	}
	
	//用户添加
	@RequestMapping("addUserLiu")
	public String addUserLiu(User user) throws Exception {
		int rows=userService.addUserLiu(user);
		if(rows>0) {
			return "redirect:selectUserAllYao.action";
		}else {
			return "redirect:error.jsp";
		}
		
	}
	
	//用户删除
	@RequestMapping("deleteUserLiu")
	public String deleteUserLiu(String id) {
		String a []  = id.split(",");
		int b=0;
		for(int i=0;i<a.length;i++) {
			Integer ids=Integer.parseInt(a[i]);
			int rows=userService.deleteUserLiu(ids);
			b+=rows;
		}
		if(b==a.length) {
			return "redirect:selectUserAllYao.action";
		}else {
			return "redirect:error.jsp";
		}		
	}
	
	//用户信息更改
	@RequestMapping("userUpdateLiu")
	public ModelAndView userUpdateLiu(Integer id) {
		User user=userService.selectUserAllYao(id);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("user",user);
		modelAndView.setViewName("/Admin/updateUser.jsp");
		return modelAndView;
	}
	
	//用户信息更改确认
	@RequestMapping("userUpdateComfirmLiu")
	public String userUpdateComfirmLiu(User user) {
		int rows=userService.userUpdateComfirmLiu(user);
		if(rows>0) {
			return "redirect:selectUserAllYao.action";
		}else {
			return "redirect:error.jsp";
		}
		
	}
}
